﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

'UNDONE: Estou ainda estudando a possibilidade de uso de certificado para registro de software em clientes
Public Class clsReProCertificado
    Private Shared _Certificado As X509Certificate2
    Public Shared Property Certificado() As X509Certificate2
        Get
            Return _Certificado
        End Get
        Private Set(ByVal value As X509Certificate2)
            _Certificado = value
        End Set
    End Property

    Private Shared _OpenedCert As Boolean
    Public Shared Property OpenedCert() As Boolean
        Get
            Return _OpenedCert
        End Get
        Set(ByVal value As Boolean)
            _OpenedCert = value
        End Set
    End Property

    Public Sub OpenCertFile()
        Dim strFileName As String = GetCertFileName()

        If Trim(strFileName) = "" Then
            Me._OpenedCert = False
            Exit Sub
        End If

        Dim oX509Cert As X509Certificate2 = New X509Certificate2(strFileName, "thepassword")

        Certificado = oX509Cert

        Me._OpenedCert = True

    End Sub


    Private Function GetCertFileName() As String
        Dim myFile As OpenFileDialog = New OpenFileDialog()
        With myFile
            myFile.Title = "Selecione um certificado válido!"
            myFile.Filter = ("Arquivos de certificado (CER) *.cer|*.cer|Arquivos de certificado (PFX) *.pfx|*.pfx|Arquivos de certificado (P7B) *.p7b|*.p7b")
            myFile.ShowDialog()
            Return .FileName
        End With
    End Function

End Class